﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ModifySeason : System.Web.UI.Page
{
    ManageDatabase data;
    Season season;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            season = new Season();

            if (data == null)
            {
                data = new ManageDatabase();
            }
            if (!IsPostBack)
            {
                GetSeries();

                if (Request.QueryString["evad"] != null)
                {
                    String s = Request.QueryString["evad"].ToString();
                    InitializeEdit(Convert.ToInt32(s));
                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }
                Page.Form.Attributes.Add("enctype", "multipart/form-data"); 
            }
        }
        else
        {
            Response.Redirect("~/Default.aspx");
        }
    }

    private void GetSeries()
    {
        ListItemCollection allSeries = data.GetSeriesSimple();
        AddSeasonChooseSeries.Items.Clear();
        AddSeasonChooseSeries.Items.Add(new ListItem("--Válasszon sorozatot--", "0"));
        foreach (ListItem item in allSeries)
        {
            AddSeasonChooseSeries.Items.Add(item);
        }
    }

    protected void SeasonModify_Click(object sender, EventArgs e)
    {
        try
        {
            if (Request.QueryString["evad"] != null)
            {
                String s = Request.QueryString["evad"].ToString();
                season.id = Convert.ToInt32(s);
            }
            else {
                return;
            }
            if (AddSeasonChooseSeries.SelectedValue == "0")
            {
                Alert.Show("Kérem válasszon ki egy sorozatot!");
                return;
            }
            else
            {
                season.seriesId = Convert.ToInt32(AddSeasonChooseSeries.SelectedValue);
            }
            try
            {
                season.season = Convert.ToInt32(AddSeasonNumber.Text);
            }
            catch (Exception)
            {
                Alert.Show("Az évad száma csak numerikus lehet!");
                return;
            }

            if (AddSeasonReleaseDate.Text != "")
            {
                try
                {
                    season.releaseDate = DateTime.Parse(AddSeasonReleaseDate.Text);
                }
                catch (Exception)
                {
                    Alert.Show("Érvénytelen a megjelenés dátuma!");
                    return;
                }
            }
            season.videoUrl = AddSeasonEmbedVideo.Text;
            season.description = AddSeasonDescription.Text;

            bool ImgWasChanged = false;
            season.imgUrl = "";
            if (AddSeasonKeepImgCheck.Checked)
            {
                if (AddSeasonImg.HasFile)
                {
                    try
                    {
                        if (AddSeasonImg.PostedFile.ContentType == "image/pjpeg" || AddSeasonImg.PostedFile.ContentType == "image/x-jpeg") //image/gif,image/x-png
                        {
                            if (AddSeasonImg.PostedFile.ContentLength < 512000)
                            {
                                string filename = "season" + String.Format("{0:yyyyMMddhhmmss}", DateTime.Now) + ".jpeg";
                                AddSeasonImg.SaveAs(Server.MapPath("~/Pic/") + filename);
                                season.imgUrl = filename;
                                ImgWasChanged = true;
                            }
                            else
                            {
                                StatusLabel.Text = "A maximális képméret 500 kb!";
                                return;
                            }
                        }
                        else
                        {
                            StatusLabel.Text = "Az alábbi képformátum nem támogatott: " + AddSeasonImg.PostedFile.ContentType;
                            return;
                        }
                    }
                    catch (Exception)
                    {
                        StatusLabel.Text = "Hiba történt a kép feltöltése során.";
                        return;
                    }
                }
            }
            else
            {
                ImgWasChanged = false;
            }

            Boolean success = data.UpdateSeason(season, ImgWasChanged);

            if (!success)
            {
                Alert.Show("Hiba történt az éved létrehozása során!");
            }
            else
            {
                Response.Redirect("Series.aspx");
            }

        }
        catch (Exception)
        {
            Alert.Show("Hiba történt az éved létrehozása során!");
        }
    }
    protected void SeasonCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Series.aspx");
    }

    private void InitializeEdit(int id)
    {
        season = data.GetWholeSeason(id, true);
        if (!User.IsInRole("admin") && Functions.GetUserId() != season.userID && season.status != 1)
        {
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            AddSeasonChooseSeries.Items.FindByValue(season.seriesId.ToString()).Selected = true;
            AddSeasonNumber.Text = season.season.ToString();
            AddSeasonDescription.Text = season.description;
            AddSeasonEmbedVideo.Text = season.videoUrl;
            DateTime nullDate = new DateTime(1800, 01, 01);
            if (!season.releaseDate.Equals(nullDate))
            {
                AddSeasonReleaseDate.Text = String.Format("{0:yyyy-MM-dd}", season.releaseDate);
            }
            if (!String.IsNullOrEmpty(season.imgUrl))
            {
                AddSeasonKeepImg.ImageUrl = "~/Pic/" + season.imgUrl;
            }
        }
    }

    protected void AddSeasonKeepImgCheck_CheckedChanged(object sender, EventArgs e)
    {
        if (AddSeasonKeepImgCheck.Checked)
        {
            AddSeasonKeepImgDiv.Visible = false;
            AddSeasonChangeImgDiv.Visible = true;
        }
        else
        {
            AddSeasonChangeImgDiv.Visible = false;
            AddSeasonKeepImgDiv.Visible = true;
        }
    }
}